package com.nf.dao.impl;

import com.nf.dao.SeatInfoDao;
import com.nf.entity.SeatInfo;
import com.nf.entity.ShowInfo;
import com.nf.util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class SeatInfoDaoImpl implements SeatInfoDao {

    @Override
    public List<SeatInfo> selectByRow(int row,int showId) {
        List<SeatInfo> list=new ArrayList<>();

        Connection conn=DBUtil.getConnection();
        String sql="select * from seatInfo where seat_row=? and show_id=?";

        PreparedStatement pstmt=null;
        ResultSet rst=null;
        try {
            pstmt=conn.prepareStatement(sql);
            pstmt.setInt(1,row);
            pstmt.setInt(2,showId);

            rst=pstmt.executeQuery();
            while (rst.next()){
                int seatId=rst.getInt(1);
                int seatRow=rst.getInt(2);
                int seatCol=rst.getInt(3);
                int status=rst.getInt(4);
                int hallId=rst.getInt(5);
                int showid=rst.getInt(6);

                SeatInfo seat=new SeatInfo(seatId,seatRow,seatCol,status,hallId,showid);
                list.add(seat);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            DBUtil.close(rst,pstmt,conn);
        }
        return list;
    }

    @Override
    public int updadeStatus(int showId, int seatId,int status) {
        int r=0;

        Connection conn=DBUtil.getConnection();
        String sql="update seatInfo set status=? where show_id=? and seat_id=?";
        PreparedStatement pstmt=null;
        try {
            pstmt=conn.prepareStatement(sql);
            pstmt.setInt(1,status);
            pstmt.setInt(2,showId);
            pstmt.setInt(3,seatId);

            r=pstmt.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            DBUtil.close(null,pstmt,conn);
        }

        return r;
    }

    @Override
    public int add(SeatInfo seat) {
        int r=0;

        Connection conn=DBUtil.getConnection();
        String sql="insert into seatInfo values(?,?,default,?,?)";
        PreparedStatement pstmt=null;
        try {
            pstmt=conn.prepareStatement(sql);
            pstmt.setInt(1,seat.getSeatRow());
            pstmt.setInt(2,seat.getSeatCol());
            pstmt.setInt(3,seat.getHallId());
            pstmt.setInt(4,seat.getShowId());

            r= pstmt.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            DBUtil.close(null,pstmt,conn);
        }

        return r;
    }

    @Override
    public List<SeatInfo> selectByShowId(int showId, int status) {
        List<SeatInfo> list=new ArrayList<>();

        Connection conn=DBUtil.getConnection();
        String sql="select * from seatInfo where show_id=? and status=?";
        PreparedStatement pstmt=null;
        ResultSet rst=null;
        try {
            pstmt=conn.prepareStatement(sql);
            pstmt.setInt(1,showId);
            pstmt.setInt(2,status);
            rst= pstmt.executeQuery();
            while (rst.next()){
                int seatId=rst.getInt(1);
                int seatRow=rst.getInt(2);
                int seatCol=rst.getInt(3);
                int hallId=rst.getInt(5);
                int showid=rst.getInt(6);

                SeatInfo seat=new SeatInfo(seatId,seatRow,seatCol,status,hallId,showid);
                list.add(seat);
            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            DBUtil.close(rst,pstmt,conn);
        }
        return list;
    }

    @Override
    public int delete(int showId, int row) {
        int r=0;

        Connection conn=DBUtil.getConnection();
        String sql="delete from seatInfo where show_id=? and seat_row=?";
        PreparedStatement pstmt=null;
        try {
            pstmt=conn.prepareStatement(sql);
            pstmt.setInt(1,showId);
            pstmt.setInt(2,row);
            r= pstmt.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            DBUtil.close(null,pstmt,conn);
        }
        return r;
    }
}
